#include "TextureManager.h"

TextureManager::TextureManager(void)
{
}
TextureManager::~TextureManager(void)
{
}
bool TextureManager::Initialize(string textureFolderPath)
{
	m_basePath = path(textureFolderPath);
	if(!exists(m_basePath))
		return false;
	return true;
}
uint TextureManager::LoadTexture(string textureFilename)
{
 	path texturePath = path(m_basePath.string() + textureFilename);
	if(!exists(texturePath))
	{
		texturePath = path(m_basePath.string() + "\\" + textureFilename);
		if(!exists(texturePath))
		{
			return NULL;
		}
	}

	if(!is_regular_file(texturePath))
		return NULL;

	Texture *pNewTex = new Texture(texturePath.string());
	if(!pNewTex->IsValidTexture())
		return NULL;

	uint nTextureName = pNewTex->GetTextureName();
	m_pTextures[nTextureName] = pNewTex;
	return nTextureName;
}
void TextureManager::ReleaseTexture(uint textureID)
{
	map<uint, Texture*>::iterator it = m_pTextures.find(textureID);
	if(it == m_pTextures.end())
		return;
	delete it->second;
	m_pTextures.erase(it);
}
Texture *TextureManager::GetTextureObject(uint textureID)
{
	return m_pTextures[textureID];
}
void TextureManager::ReleaseAllTextures()
{
	map<uint, Texture*>::iterator it = m_pTextures.begin();
	for(; it != m_pTextures.end(); it++)
	{
		delete it->second;
	}
	m_pTextures.clear();
}
bool TextureManager::IsValidTexture(uint textureID) const
{
	map<uint, Texture*>::const_iterator it = m_pTextures.find(textureID);
	if(it == m_pTextures.end())
		return false;
	return true;
}